requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    Constant.initHandlebars(Handlebars);

    var url = {
        queryProductForm: "manager/queryProductFormCustom.do",
        selectLoanTypeList: "manager/selectLoanTypeList.do",
        selectApplyTermList: "manager/selectApplyTermList.do",
        selectOrderSourceList: "manager/selectOrderSourceList.do",
        selectRepanymentWayList: "manager/selectRepanymentWayList.do",
        selectLoanMethodList: "manager/selectLoanMethodList.do",

        queryLoanDefine: "manager/queryLoanDefineByProductId.do",
        //queryFeeDefine:"manager/queryFeeDefineByProductId.do",

        queryFeeDefineAll: "manager/queryFeeDefineAll.do",
        queryFeeRelation: "manager/queryFeeRelationByProductId.do",
        insertProductFeeRelation: "manager/insertProductFeeRelationInfo.do",
        insertLoanMethodDefineNew: "manager/insertProductLoanMethodInfo.do"
    }

    /************************************product_apply_message  Tab部分*******************************/
    //tab切换

    site.send({
        url: url.queryFeeDefineAll,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == 1) {
                var source = $("#fee_list_tpl").html();
                var template = Handlebars.compile(source);
                var html = template(data.content);
                $('#fee_list').html(html);
            }
        }
    })

    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == 1) {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var html = template(data.content);
                $('#loanMethod_list').html(html);
            }
        }
    })

    $(".count").on('click', function () {
        $(".fee_winBox").show();
    })

    $("#loanM").on('click', function () {
        $(".loanMethod_winBox").show();
    })

    $(document).on('click', '.icon-wrong.headerIcon', function () {
        $(this).parents('.fee_winBox').hide();
    })

    //扣费明细选择弹框确定
    $('.confirm').on('click', function () {
        var arr = new Array();
        var cb = $('#fee_list input[type="checkbox"]:checked');
        $.each(cb, function (i, e) {
            arr.push({
                fee_id: $(e).data('id'),
                fee_name: $(e).val(),
                type: $(e).data('type')
            });
        })
        site.send({
            url: url.insertProductFeeRelation,
            data: {
                frontData: JSON.stringify({
                    productFeeRelations: arr,
                    product_id: getAurlparam('productId')
                })
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    $(".fee_winBox").hide();
                    //location.reload();
                    site.send({
                        url: url.queryFeeRelation,
                        data: {
                            product_id: getAurlparam('productId')
                        },
                        callback: function (data) {
                            data = JSON.parse(data);
                            if (data.type == 1) {
                                var html = "<i>\n" +
                                    "                        <label class=\"checkbox inline\" style=\"height: auto; padding:5px 0 0 20px; box-shadow: none\">\n" +
                                    "                            <input type=\"checkbox\" name=\"leipiNewField\" title=\"复选框\" value=\"选项1\" orginline=\"inline\" style=\"width: 15px;\">利率</label><label class=\"text slabel isDisabled\" style=\"display: block\"><input type=\"text\" class=\"ng-pristine ng-untouched ng-valid ng-empty\">分</label></i><i style=\"display: none\"><label class=\"checkbox inline\" style=\"height: auto; padding:5px 0 0 20px; box-shadow: none\"><input type=\"checkbox\" name=\"leipiNewField\" title=\"复选框\" value=\"选项1\" class=\"leipiplugins\" leipiplugins=\"checkbox\" orginline=\"inline\" style=\"width: 15px\">\n" +
                                    "                        </label>\n" +
                                    "                        <label class=\"text slabel isDisabled\" style=\"display: block\">\n" +
                                    "                            <input type=\"text\">\n" +
                                    "                        </label>\n" +
                                    "                    </i>";
                                $.each(data.content, function (i, e) {
                                    var type = e.type == 1 ? '元' : '分';
                                    html += '<i><label class="checkbox inline"  id="' + e.fee_id + '" style="height: auto; padding:5px 0 0 20px; box-shadow: none"><input type="checkbox"  name="leipiNewField" title="复选框" orginline="inline" style="width: 15px;">' + e.fee_name + '</label><label class="text slabel isDisabled" style="display: block"><input type="text" key="' + e.english_name + '" class="ng-pristine ng-untouched ng-valid ng-empty">' + type + '</label></i>';
                                })
                                $("#count").html(html);
                            }
                        }
                    })
                }
            }
        })
    })

    //贷款方式选择弹框确定
    $('.loanConfirm').on('click', function () {
        var arr = new Array();
        var cb = $('#loanMethod_list input[type="checkbox"]:checked');
        $.each(cb, function (i, e) {
            arr.push({
                loan_method_id: $(e).data('id'),
                name: $(e).val(),
            });
        })
        site.send({
            url: url.insertLoanMethodDefineNew,
            data: {
                /*loanMehtodDefines: JSON.stringify(arr),
                product_id: getAurlparam('productId')*/

                frontData: JSON.stringify({
                    loanMethodDefines: arr,
                    product_id: getAurlparam('productId')
                })
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    $(".loanMethod_winBox").hide();
                    //pjaxReload();
                    site.send({
                        url: url.queryLoanDefine,
                        data: {
                            product_id: getAurlparam('productId')
                        },
                        callback: function (data) {
                            data = JSON.parse(data);
                            if (data.type == 1) {
                                var source = $("#loanMethod_content_tpl").html();
                                var template = Handlebars.compile(source);
                                var html = template(data.content);
                                $('#loanMethod_content').html(html);
                                /*var html = "<i>\n" +
                                    "                        <label class=\"checkbox inline\" style=\"height: auto; padding:5px 0 0 20px; box-shadow: none\">\n" +
                                    "                            <input type=\"checkbox\" name=\"leipiNewField\" title=\"复选框\" value=\"选项1\" orginline=\"inline\" style=\"width: 15px;\">利率</label><label class=\"text slabel isDisabled\" style=\"display: block\"><input type=\"text\" class=\"ng-pristine ng-untouched ng-valid ng-empty\">分</label></i><i style=\"display: none\"><label class=\"checkbox inline\" style=\"height: auto; padding:5px 0 0 20px; box-shadow: none\"><input type=\"checkbox\" name=\"leipiNewField\" title=\"复选框\" value=\"选项1\" class=\"leipiplugins\" leipiplugins=\"checkbox\" orginline=\"inline\" style=\"width: 15px\">\n" +
                                    "                        </label>\n" +
                                    "                        <label class=\"text slabel isDisabled\" style=\"display: block\">\n" +
                                    "                            <input type=\"text\">\n" +
                                    "                        </label>\n" +
                                    "                    </i>";
                                $.each(data.content, function (i, e) {
                                    var type = e.type == 1 ? '元' : '分';
                                    html += '<i><label class="checkbox inline"  id="' + e.fee_id + '" style="height: auto; padding:5px 0 0 20px; box-shadow: none"><input type="checkbox"  name="leipiNewField" title="复选框" orginline="inline" style="width: 15px;">' + e.fee_name + '</label><label class="text slabel isDisabled" style="display: block"><input type="text" key="' + e.english_name + '" class="ng-pristine ng-untouched ng-valid ng-empty">' + type + '</label></i>';
                                })
                                $("#loanM").html(html);*/
                            }
                        }
                    })
                }
            }
        })
    })


    site.send({
        url: url.queryFeeRelation,
        data: {
            product_id: getAurlparam('productId')
        },
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == 1) {
                var html = "";
                $.each(data.content, function (i, e) {
                    var type = e.type == 1 ? '元' : '分';
                    html += '<i><label class="checkbox inline"  id="' + e.fee_id + '" style="height: auto; padding:5px 0 0 20px; box-shadow: none"><input type="checkbox"  name="leipiNewField" title="复选框" orginline="inline" style="width: 15px;">' + e.fee_name + '</label><label class="text slabel isDisabled" style="display: block"><input type="text" key="' + e.english_name + '" class="ng-pristine ng-untouched ng-valid ng-empty">' + type + '</label></i>';
                    //console.log($('.fee_winBox input[data-id=' + e.fee_id + ']'));
                    $('.fee_winBox input[data-id=' + e.fee_id + ']').prop('checked', true);
                })
                $("#count").append(html);
            }
        }
    })


    $("#mess_add_form").on('click', function () {
        $("#apply-message").append("<iframe id='form_frame' src='OperaManager/product/common/message_form_design.html' width='100%' height='750px' style='overflow: hidden; margin-top: 20px;' frameborder='0' ></iframe>");
    })

    $('.public-PoBefore-nav li a').on('click', function () {
        var params = $(this).data('type');
        if (params == 'Reimbursement' || params == 'PersonMess' || params == 'message' || params == 'dataList') {
            //Reimbursement=还款明细
            //message=借款信息
            //personMess=借款人資料
            //dataList=資料清單
            //切换菜单
            $('.public-PoBefore-nav li a').removeClass('activePublic');
            $(this).addClass('activePublic');

            //切换内容
            $('.public-content').hide();
            $('#apply-' + params).show();
        }
    });

    //日期控件激活
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true,
        pickerPosition: 'bottom-left'
    })

    function initFormSelect() {
        site.send({
            url: url.selectLoanTypeList,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#loanType_content_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $('#loanType_content').append(html);
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            }
        })

        site.send({
            url: url.selectApplyTermList,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#applyTerm_content_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $('#applyTerm_content').append(html);
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            }
        })

        site.send({
            url: url.selectRepanymentWayList,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#repanymentWay_content_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $('#repanymentWay_content').append(html);
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            }
        })

        site.send({
            url: url.selectOrderSourceList,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#orderSource_content_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $('#orderSource_content').append(html);
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            }
        })

        site.send({
            url: url.queryLoanDefine,
            data: {
                product_id: getAurlparam('productId')
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    var source = $("#loanMethod_content_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $('#loanMethod_content').html(html);
                }
                else {
                    Constant.fn.Message("F", data.msg)
                }
            }
        })

        /*site.send({
            url: url.queryLoanDefine,
            data: {
                product_id: getAurlparam('productId')
            },
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == 1) {
                    //var html = "<label style=\"display: none\" class=\"radio inline\"><input type=\"radio\" name=\"leipiNewField\" title=\"单选框\" value=\"选项1\" class=\"leipiplugins\" leipiplugins=\"radio\" key=\"1\" orginline=\"inline\" style=\"width: 15px\"></label>\n";
                    var html = "<label style=\"display:none\" class=\"radio inline\"><input type=\"radio\" leipiplugins=\"radio\" name=\"leipiNewField\"  title=\"单选框\"  orginline=\"inline\" class=\"leipiplugins\" style=\"width: 15px\"></label>\n";
                    if (data.content.length > 0) {
                        $("#loanwayadd").hide();
                        $.each(data.content, function (i, e) {
                            html += '<label class="radio inline">\n<input type="radio" leipiplugins="radio"  name="leipiNewField" loan_mehtod_id="' + e.id + '" title="单选框" value="' + e.name + '" orginline="inline" key="' + e.english_name + '" class="leipiplugins" style="width: 15px">' + e.name + '\n</label>';
                        })
                    }
                    else {
                        $("#loanwayadd").show();
                    }
                    $("#loanway").html(html);
                }
            }
        })*/

        /*site.send({
            url:url.queryFeeDefine,
            data:{
                product_id:Constant.getAurlparam('productId')
            },
            callback:function(data){
                data = JSON.parse(data)
                if(data.type ==1){
                    var html = "<span><label class=\"checkbox inline\" style=\"height: auto; padding:5px 0 0 20px; box-shadow: none\"><input type=\"checkbox\" name=\"leipiNewField\" title=\"复选框\" value=\"选项1\" orginline=\"inline\" style=\"width: 15px;\">利率</label><label class=\"text slabel isDisabled\" style=\"display: block\"><input type=\"text\" class=\"ng-pristine ng-untouched ng-valid ng-empty\">分</label></span><i style=\"display: none\"><label class=\"checkbox inline\" style=\"height: auto; padding:5px 0 0 20px; box-shadow: none\"><input type=\"checkbox\" name=\"leipiNewField\" title=\"复选框\" value=\"选项1\" class=\"leipiplugins\" leipiplugins=\"checkbox\" orginline=\"inline\" style=\"width: 15px\"></label><label class=\"text slabel isDisabled\" style=\"display: block\"><input type=\"text\"></label></i>\n";
                    if(data.content){
                        //$("#loanwayadd").toggle();
                        $.each(data.content,function(i,e){
                            html += '<i><label class="checkbox inline"  id="'+e.id+'" style="height: auto; padding:5px 0 0 20px; box-shadow: none"><input type="checkbox"  name="leipiNewField" title="复选框" orginline="inline" style="width: 15px;">'+e.name+'</label><label class="text slabel isDisabled" style="display: block"><input type="text" key="'+e.english_name+'" class="ng-pristine ng-untouched ng-valid ng-empty">'+e.type+'</label></i>';
                        })
                    }
                    $("#count").html(html);
                }
            }
        })*/
    }

    initFormSelect();
    //初始化上半部分固定表单下拉框和数据


    site.send({
        url: url.queryProductForm,
        data: {
            product_id: getAurlparam('productId')
        },
        callback: function (data) {
            data = JSON.parse(data);
            /*var source = $("#form_yourself_tpl").html();
            var template = Handlebars.compile(source);
            var list = data.content.productPropertyCategorys[2];
            var html = template(list);
            $('#form_yourself').html(html);*/
            if (data.type == 1) {
                //渲染客户自定义部分
                if (data.content.personalPropertyCategorys) {
                    $.each(data.content.personalPropertyCategorys, function (index, item) {
                        /*if(index == 0){
                            $('#add_form').before(item.template_html);
                        }*/
                        //else{
                        $('#apply-PersonMess').append("<iframe id='form_frame' category_id='" + item.category_id + "' src='OperaManager/product/common/personMess_form_design.html?category_id=" + item.category_id + "' width='100%' height='750px' style='overflow: hidden; margin-top: 20px;' frameborder='0' ></iframe>")
                        //}
                    })
                }

                //渲染产品自定义部分
                if (data.content.productPropertyCategorys) {
                    $.each(data.content.productPropertyCategorys, function (index, item) {
                        /*if(index == 0){
                            $('#add_form').before(item.template_html);
                        }*/
                        //else{
                        $('#apply-message').append("<iframe id='form_frame' category_id='" + item.category_id + "' src='OperaManager/product/common/message_form_design.html?category_id=" + item.category_id + "' width='100%' height='750px' style='overflow: hidden; margin-top: 20px; ' frameborder='0' ></iframe>")
                        //}
                    })

                }
            }
            else {
                Constant.fn.Message("F", data.msg)
            }
        }, callError: function () {

        }
    })

    /**************************  fee_winBox的搜索功能    ******************************/
    $('#feeSearch').on('click', function () {
        var data = {};
        if ($("#keyWordInput").val()) {
            data.keyword = $("#keyWordInput").val();
        }
        site.send({
            url: url.queryFeeDefineAll,
            data: data,
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type === 1) {
                    var source = $("#fee_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var html = template(data.content);
                    $('#fee_list').html(html);
                }
            }
        })
    })

    /**************************  fee_winBox的搜索功能    ******************************/


    //固定字段部分
    //弹窗
    /*end***********************************product_apply_message  Tab部分*******************************/


    /************客户自定义    product_apply_PersonMess  部分********/
    //性别选中
    $('.sex-radio').on('click', function () {
        $('.sex-radio').removeClass('is-checked');
        $(this).addClass('is-checked');
    })


    //实现智能联想
    var searData = {};

    var Intelligent_association = {

        creSele: function (obj) {
            var autocomplete = $(obj.el).autocomplete({
                hints: obj.data,
                width: obj.width,
                height: obj.height,
                val: obj.val,
                inputClass: obj.inputClass,
                placeholder: obj.placeholder,
                onSubmit: function (text) {
                    obj.backball(text)
                }
            })
            return autocomplete
        },

        initHuArea: function (idxData) {
            var _this = this;
            var huArea = _this.creSele({						//户口所在地
                el: '.huArea',							//页面上的span节点
                data: '',								//需要搜索的所有数据
                width: 158,
                height: 32,
                val: idxData,									//默认input值
                inputClass: 'huInput',					//把动态生成的input class写进去 以免影响插件原有class
                placeholder: '请输入检索城市名称',
                backball: function (text) {				//用户选择后返回的值
                    searData.huArea = text
                }
            })
            $('.huInput').bind('keyup keydown', function (e) {		//监听用户敲击键盘时搜索数据
                if (e.which > 32 && e.which < 126) return
                if (/^[\u4e00-\u9fa5]*$/.test($(this).val())) {
                    huArea.hints = _this.getCity()				//赋值数据进插件对象huArea
                }
            })
        },

        initHomeArea: function (idxData) {
            var _this = this
            var homeArea = _this.creSele({
                el: '.homeArea',							//页面上的span节点
                data: '',								//需要搜索的所有数据
                width: 158,
                height: 32,
                val: idxData,							//默认input值
                inputClass: 'homeInput',					//把动态生成的input class写进去 以免影响插件原有class
                placeholder: '请输入检索城市名称',
                backball: function (text) {				//用户选择后返回的值
                    _this.searData.homeArea = text
                }
            })
            $('.homeInput').bind('keyup keydown', function (e) {		//监听用户敲击键盘时搜索数据
                if (e.which > 32 && e.which < 126) return
                if (/^[\u4e00-\u9fa5]*$/.test($(this).val())) {
                    homeArea.hints = _this.getCity()						//赋值数据进插件对象huArea
                }
            })
        },

        getCity: function () {
            var city;
            $.ajax({
                type: 'get',
                url: 'static/cityjson.json',
                //async:false
                success: function (data) {
                    city = data
                }
            })
            return city
        }
    }

    Intelligent_association.initHuArea()
    Intelligent_association.initHomeArea()

    $("#PersonMess_add_form").on('click', function () {
        $("#apply-PersonMess").append("<iframe id='form_frame' src='OperaManager/product/common/personMess_form_design.html' width='100%' height='750px' style='overflow: hidden; margin-top: 20px; ' frameborder='0' ></iframe>");
    })

    /************************************product_apply_PersonMess  Tab部分*******************************/

})